package com.xq.tmall.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xq.tmall.agu.core.agent.WebAgent;
import com.xq.tmall.agu.openapi.TushareApi;
import com.xq.tmall.agu.task.EasyCrawl;
import com.xq.tmall.dao.StockCodeInfoDao;
import com.xq.tmall.dao.StockDao;
import com.xq.tmall.entity.Stock;
import com.xq.tmall.entity.StockCodeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 代码信息服务类
 * @author chenguoxiang
 * @create 2018-10-30 15:22
 **/
@Slf4j
@Service
public class StockCodeInfoService {

    @Autowired
    private TushareApi tushareSpider;

    @Resource
    private StockCodeInfoDao stockCodeInfoDao;

    @Resource
    private StockDao stockDao;


    /**
     * 代码列表刷新
     */
    public void refreshCode( Map<String, Object> sds ){
        List<Stock> list = new ArrayList<>();
        for(int j = 0; j <sds.size();j++){
            String result = JSON.toJSONString(sds.get(j));
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray jsonArray = data.getJSONArray("diff");
            for (int i = 0; i < jsonArray.size(); i++) {
                Stock stock = new Stock(jsonArray.getJSONObject(i));
                Stock stock1 = stockDao.selectByPrimaryKey(stock.getCode());
                if(stock1 == null){
                    list.add(stock);
                }
            }

        }
        stockDao.batchInsert(list);
    }

    /**
     * @param params
     * @return
     */
    public List<StockCodeInfo> findStockCodeInfoByParam(Map<String,Object> params) {
        return stockCodeInfoDao.findStockCodeInfo(params);

    }

    /**
     * 更新单条数据
     * @param stockCodeInfo
     * @return
     */
    public int updateByPrimaryKeySelective(StockCodeInfo stockCodeInfo){
        return stockCodeInfoDao.updateByPrimaryKeySelective(stockCodeInfo);
    }

    /**
     * 删除数据
     * @param code
     * @return
     */
    public int deleteByPrimaryKey(String code){
        return stockCodeInfoDao.deleteByPrimaryKey(code);
    }

    /**
     * 返回所有代码
     *
     * @return
     */
    public List<StockCodeInfo> getAllStockCodes() {
        Map<String,Object> params = new HashMap<>();
        return stockCodeInfoDao.findStockCodeInfo(params);

    }

    /**
     * 返回沪市代码
     * @return
     */
    public List<StockCodeInfo> getSHStockCodes() {
        Map<String,Object> params = new HashMap<>();
        params.put("type","sh");
        return stockCodeInfoDao.findStockCodeInfo(params);
    }

    /**
     * 返回深市代码
     * @return
     */
    public List<StockCodeInfo> getSZStockCodes() {
        Map<String,Object> params = new HashMap<>();
        params.put("type","sz");
        return stockCodeInfoDao.findStockCodeInfo(params);
    }
}
